﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SmallERP.Bll.PrintManage;
using SmallERP.MODEL.PrintManage;
using SmallERP.COM;
using SmallERP.MODEL.FoundationModel;
using SmallERP.Bll.Com;

namespace SmallERP.PrintManage
{
    public partial class FrmPaperManage1 : Form
    {
        #region 私有成员
        private PaperInfoModel _paperInfo = null;
        private  PaperBLL _bll = new PaperBLL();
        #endregion

        #region 公共成员

        public delegate void ChangePaperCompleteHander();
        public event ChangePaperCompleteHander ChangePaperCompleteEvent;
        #endregion

        #region 属性
        #endregion

        public FrmPaperManage1()
        {
            InitializeComponent();
        }

   
        private void FrmPaperManage_Load(object sender, EventArgs e)
        {
            LinkPaperFace();
            LinkPaperType();
            LinkGridControl(((FrmMain)this.Owner).Printer.PrinterID);
        }


        /// <summary>
        /// 绑定GridControl
        /// </summary>
        private void LinkGridControl(int printerId)
        {
            
            gridControlPaperInfo.DataSource = _bll.GetPaperInfoList(printerId);
            gridViewPaperInfo.RefreshData();
        }
        /// <summary>
        /// 绑定纸面
        /// </summary>
        private void LinkPaperFace()
        {
            PaperFaceBLL paperFacebll = new PaperFaceBLL();
            cmbPaperFace.Properties.Items.Clear();
            foreach (var item in paperFacebll.GetPaperFaceList())
            {
                cmbPaperFace.Properties.Items.Add(item.PaperFaceName);
            }
        }
        /// <summary>
        /// 绑定纸型
        /// </summary>
        private void LinkPaperType()
        {
            SmallERP.Bll.PrintManage.PaperTypeBLL paperTypeBLL = new SmallERP.Bll.PrintManage.PaperTypeBLL();
            cmbPaperType.Properties.Items.Clear();
            foreach (var item in paperTypeBLL.GetPaperTypeList())
            {
                cmbPaperType.Properties.Items.Add(item.PaperTypeName);
            }
        }
        /// <summary>
        /// 添加纸
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (btnAdd.Text != "保存")
            {
                PrepareAdd(true);
                cmbPaperFace.Focus();
                return;
            }
            if (CheckData())
            {
                try
                {
                    IDataAccess iDataAccess = new PaperBLL();
                    PaperInfoModel paperInfo = new PaperInfoModel();
                    paperInfo.AddTime = UnityMethod.GetSystemTime();
                    paperInfo.EndPaperLength = Convert.ToDouble(txtPaperTitle.Text);
                    paperInfo.InvalidationArea = 0;
                    paperInfo.LastBordId = 0;
                    paperInfo.LastLength = Convert.ToDouble(txtLastLength.Text);
                    paperInfo.LastPrintTime = UnityMethod.GetSystemTime();
                    paperInfo.Length = Convert.ToDouble(txtLengh.Text);
                    paperInfo.PaperConsumptionLength = Convert.ToDouble(txtPaperTitle.Text);
                    paperInfo.PaperEndArea = Convert.ToDouble(txtPaperTitle.Text) * Convert.ToDouble(txtWidth.Text) / 10000;
                    paperInfo.PaperFaceID = GetPaperFace(cmbPaperFace.Text) == null ? 0 : GetPaperFace(cmbPaperFace.Text).PaperFaceId;
                    paperInfo.PaperTitleArea = 0;
                    paperInfo.PaperTypeID = GetPaperType(cmbPaperType.Text) == null ? 0 : GetPaperType(cmbPaperType.Text).PaperTypeId;
                    paperInfo.PrinterID = ((FrmMain)this.Owner).Printer.PrinterID;
                    paperInfo.Remark = string.IsNullOrEmpty(txtRemark.Text) ? "Blank " : txtRemark.Text;
                    paperInfo.Status = 0;
                    paperInfo.ValidationArea = 0;
                    paperInfo.Width = Convert.ToDouble(txtWidth.Text);
                    int ret = iDataAccess.Insert(paperInfo);
                    if (ret == 0)
                    {
                        LinkGridControl(((FrmMain)this.Owner).Printer.PrinterID);
                        PrepareAdd(false);
                        MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    }
                    else
                    {
                        MessageBox.Show("添加失败，请检查网络连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
        /// <summary>
        /// 换纸
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            int paperWasteRecord =0;
            PaperInfoModel prepaperInfo = null;
            if (_paperInfo != null)
            {
                PaperBLL paperBLL = new PaperBLL();
                if (_paperInfo.Status != (int)SmallERP.COM.Status.PaperInfoStatus.Using)
                {
                    if (_paperInfo.LastLength > 10)
                    {
                        int ret = paperBLL.ChangePaper(_paperInfo, 0, out paperWasteRecord, out prepaperInfo);

                        if (ret == 0)
                        {
                            LinkGridControl(((FrmMain)this.Owner).Printer.PrinterID);
                            ((FrmMain)this.Owner).Paper = _bll.GetPaper(_paperInfo.PrintPaperId);
                            ChangePaperCompleteEvent();
                            MessageBox.Show("换纸成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else
                        {
                            MessageBox.Show("换纸失败，请检查网络连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }


                    }
                    else
                    {
                        MessageBox.Show("该打印纸长度不够！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    MessageBox.Show("该打印纸正在使用中！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show("没有选择纸！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }

        /// <summary>
        /// 检查数据完整性
        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
            bool flag = true;
            if (cmbPaperFace.Text == string.Empty)
            {
                cmbPaperFace.Focus();
                MessageBox.Show("纸面不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (cmbPaperType.Text == string.Empty)
            {
                cmbPaperType.Focus();
                MessageBox.Show("纸型不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (txtLengh.Text == string.Empty)
            {
                txtLengh.Focus();
                MessageBox.Show("初始长度不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (txtPaperTitle.Text == string.Empty)
            {
                txtPaperTitle.Focus();
                MessageBox.Show("纸头不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (txtWidth.Text == string.Empty)
            {
                txtWidth.Focus();
                MessageBox.Show("纸宽不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (txtLastLength.Text == string.Empty)
            {
                txtLastLength.Focus();
                MessageBox.Show("剩余长度不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return flag;
        }

        /// <summary>
        /// 获得纸面对象
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        private PaperFace_Info GetPaperFace(string name)
        {
            PaperFaceBLL paperFaceBLL = new PaperFaceBLL();
            return paperFaceBLL.GetPaperFace(name);
        }

        /// <summary>
        /// 获得纸型对象
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        private PaperType_Info GetPaperType(string name)
        {
            SmallERP.Bll.PrintManage.PaperTypeBLL paperTypeBLL = new SmallERP.Bll.PrintManage.PaperTypeBLL();
            return paperTypeBLL.GetPaperType(name);
        }

        /// <summary>
        /// 添加前控件设置
        /// </summary>
        /// <param name="bl"></param>
        private void PrepareAdd(bool bl)
        {
            cmbPaperFace.Enabled = bl;
            cmbPaperType.Enabled = bl;
            txtLengh.Enabled = bl;
            txtPaperTitle.Enabled = bl;
            txtLastLength.Enabled = bl;
            txtRemark.Enabled = bl;
            txtWidth.Enabled = bl;

            cmbPaperFace.Text = string.Empty;
            cmbPaperType.Text = string.Empty;
            txtLengh.Text = string.Empty;
            txtPaperTitle.Text = string.Empty;
            txtLastLength.Text = string.Empty;
            txtRemark.Text = string.Empty;
            txtWidth.Text = string.Empty;

            if (bl)
            {

                btnAdd.Text = "保存";
                btnUpdate.Enabled = false;
            }
            else
            {
                btnAdd.Text = "添加";
                btnUpdate.Enabled = true;
            }
        }

      

        private void btnCancel_Click(object sender, EventArgs e)
        {
            cmbPaperFace.Enabled = false;
            cmbPaperType.Enabled = false;
            txtLengh.Enabled = false;
            txtPaperTitle.Enabled = false;
            txtLastLength.Enabled = false;
            txtRemark.Enabled = false;

            btnAdd.Text = "添加";
            btnAdd.Enabled = true;
            btnUpdate.Enabled = true;
        }

        private void btnAddKaZhi_Click(object sender, EventArgs e)
        {

        }

        private void btnCalibration_Click(object sender, EventArgs e)
        {

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {

        }

        private void txtPaperTitle_KeyUp(object sender, KeyEventArgs e)
        {
           txtLastLength.Text = Math.Round(((Convert.ToDouble(txtLengh.Text) * 100 - Convert.ToDouble(txtPaperTitle.Text)) / 100), 2).ToString(); 
        }

        private void gridViewPaperInfo_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            _paperInfo = (PaperInfoModel)gridViewPaperInfo.GetFocusedRow();
        }

        private void gridViewPaperInfo_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                PaperInfoModel paperInfoEntity = (PaperInfoModel)gridViewPaperInfo.GetRow(e.RowHandle);
                if (paperInfoEntity.Status == 1)
                {
                    e.Appearance.ForeColor = Color.ForestGreen;
                }
            }
        }

        
    }
}
